const cloud = require('wx-server-sdk')
const axios = require('axios');
cloud.init()

exports.main = async (event, context) => {
    const { localLng, localLat, targetLng, targetLat, zoom, color } = event;
    const ak = 'xoSxtsisj1ZZGepT5xk5evIsdEUSxy36'; // 替换为你的百度地图 API 密钥
    const hexColor = color;
    console.log("color",color);
    // 构建百度地图静态图 API 的 URL
    const url = `https://api.map.baidu.com/staticimage/v2`;
    const params = {
        ak,
        center: `${parseFloat(localLng)},${parseFloat(localLat)}`,
        zoom,
        width: 480,
        height: 300,
        scale: 2,
         markers: `${localLng},${localLat}|${targetLng},${targetLat}`,
        markerStyles: `l,A,0x00FF00|l,B,${hexColor}`
    };

    // 拼接参数到 URL
    const queryString = Object.keys(params)
      .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
      .join('&');
    const fullUrl = `${url}?${queryString}`;

    try {
        const res = await axios.get(fullUrl);
        return { url: fullUrl };
    } catch (error) {
        console.error('静态图生成失败:', error);
        throw new Error(`静态图生成失败：${error.message}`);
    }
};